package com.alpha.framework.activity;

import com.alpha.framework.R;
import com.alpha.framework.fragment.BaseFragment;
import com.alpha.framework.ui.LibFragmentTabHost;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

public abstract class BaseTabsActivity extends BaseActivity implements OnTabChangeListener{

	private LibFragmentTabHost mTabHost;
	private TabWidget mTabWidget;
	private int currentIndex;

	@SuppressLint("NewApi")
	@Override
	protected void initView() {
		setContentView(R.layout.lib_tab_layout);
		mTabHost = (LibFragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(),
				android.R.id.tabcontent);
		mTabHost.getTabWidget().setDividerDrawable(null);
		addTabs();
		mTabHost.setOnTabChangedListener(this);
	}

	/**
	 * 子类需要实现add tab的方法
	 * 如：mTabsAdapter.addTab(mTabHost.newTabSpec("custom").setIndicator
	 * ("Custom"), LoaderCourseListSupport.CourseListFragment.class, null);
	 */
	protected abstract void addTabs();

	/**
	 * 设置Tab切换
	 * @param tabIndex
	 * 切换的Tab下标
	 */
	protected void setTabChange(int tabIndex)
	{
		mTabHost.setCurrentTab(tabIndex);
	}
	
	/**
	 * 添加Tab子页面
	 * 
	 * @param tabText
	 *            Tab导航名称
	 * @param type
	 *            Tab类型
	 * @param cls
	 *            Tab页Class对象
	 * @param bundle
	 *            传递参数
	 */
	@SuppressLint("NewApi")
	protected void addTab(View tabView, Class<? extends Fragment> cls, Bundle bundle)
	{
		mTabHost.addTab(
		        mTabHost.newTabSpec(cls.getSimpleName()).setIndicator(tabView),
		        cls, bundle);
	}
	
	/**
	 * 获取当前Tab下标
	 * 
	 * @return Tab下标
	 */
	protected int getTabPosition()
	{
		return currentIndex;
	}
	
	@Override
	public void onTabChanged(String tabId) {
		currentIndex = mTabHost.getCurrentTab();
	}

	
}
